package com.gxuwz.jwt.controller;

import com.alibaba.fastjson.JSONObject;
import com.gxuwz.jwt.annotation.CheckToken;
import com.gxuwz.jwt.annotation.LoginToken;
import com.gxuwz.jwt.entity.User;
import com.gxuwz.jwt.service.UserService;
import com.gxuwz.jwt.util.JwtUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
public class UserController {

    @Autowired
    private UserService userService;

    @LoginToken
    @GetMapping("/login")
    public Object login(@RequestParam("userId")String userId,@RequestParam("userName")String userName,@RequestParam("password")String password){
        JSONObject jsonObject = new JSONObject();
        User userInfo = userService.getUserById(userId);
        if (userInfo == null){
            jsonObject.put("message","用户不存在aa");
            return jsonObject;
        }
        else {
            if (!userInfo.getPassword().equals(password)){
                jsonObject.put("message","密码错误");
                return jsonObject;
            }
            else {
                String token = JwtUtil.createJWT(7000000,userInfo);
                jsonObject.put("token",token);
                jsonObject.put("user",userInfo);
                return jsonObject;
            }
        }
    }

    @CheckToken
    @GetMapping("/getUserInfo")
    public String getUserInfo(){
        return "你好";
    }

    @CheckToken
    @GetMapping("/purchase")
    public String purchase(){
        return "购买成功";
    }

}
